package de.kieselbach.javafx.builder.factory

import javafx.scene.Scene
import javafx.scene.layout.BorderPane
import de.kieselbach.javafx.builder.factory.AbstractBuilderFactory

/*
 * Copyright 2012 by procar informatik AG,
 * Eschenweg 7, 64331 Weiterstadt, Germany
 * All rights reserved.
 */
class SceneBuilderFactory extends AbstractBuilderFactory {

    @Override
    String getName() {

        return 'scene'
    }

    @Override
    Class getNodeClass() {

        return Scene.class
    }

    @Override
    void nodeCompleted(Object parent, Object child) {

        parent.root.center = child
    }

    @Override
    def createNode(Object name, Map attributes, Object value) {

        def width = null
        def height = null
        if(attributes != null) {
            width = attributes['width']
            height  = attributes['height']
        }

        def scene = width > 0 && height > 0 ? new Scene(new BorderPane(), width, height) : new Scene(new BorderPane())

        if(attributes != null) {
            attributes.each() {key, val ->

                if(key == 'stylesheet') {
                    if(val) {
                        scene.stylesheets.add(val);
                    }
                }
                else if(key == 'stylesheets') {
                    if(val) {
                        scene.stylesheets.addAll(val);
                    }
                }
                else if(key != 'width' && key != 'height') {
                    scene[key] = val
                }
            }
        }

        scene
    }
}
